package p2p.whiteboard
{
	public class MessageObject extends Object
	{
		protected var _data:Object;
		protected var _sender:String; // the sender's nearID
		protected var _destination:String; // the destination ID
		
		public function MessageObject(data:Object, senderId:String)
		{
			_data    = data;
			_sender  = senderId;
		}

		public function get destination():String
		{
			return _destination;
		}

		public function set destination(value:String):void
		{
			_destination = value;
		}

		public function get sender():String
		{
			return _sender;
		}

		public function get data():Object
		{
			return _data;
		}

		public function getObjectToSend():Object
		{
			var o:Object = {};
			o.data      = _data;
			o.sender    = _sender;
			o.salt = Math.random()*99999; // We add some salt to make sure we do not send the same message 2 times
			
			if(destination)
			{
				o.destination = destination;
			}
			
			return o;
		}
		
		public static function parse(obj:Object):MessageObject
		{
			var msg:MessageObject = new MessageObject(obj.data, obj.sender);
			
			if(obj.hasOwnProperty("destination"))
			{
				msg.destination = obj.destination;
			}
			
			return msg;
		}
	}
}